package cn.xz.mxxz

import cn.xz.mxxz.activity.*
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.bean.CommonLib
import cn.xz.mxxz.bean.DateLogin
import cn.xz.mxxz.databinding.ActivityMainBinding
import cn.xz.mxxz.dialog.AllClickDialog
import cn.xz.mxxz.dialog.LoseAppDialog
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.util.ARouter
import cn.xz.mxxz.util.SessionUtils
import java.text.DateFormat
import java.text.SimpleDateFormat

/**
 * 1920 * 1200*/
class MainActivity: BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

    //当前Logo点击次数
    private var logoNum = 0
    //上次点击logo时间
    private var lastLogoTime = 0L

    override fun initData() {
    }

    override fun initEvent() {
        //Logo
        mDataBind.ivLogo.setOnClickListener {
            //连点五次进入数据下载界面
            if (System.currentTimeMillis() - lastLogoTime > 2000) {
                logoNum = 0
                lastLogoTime = System.currentTimeMillis()
            } else {
                logoNum++
                if (logoNum >= 5) {
                    ARouter.to(LoadingActivity::class.java)
                }
            }
        }
        //一件全开
        mDataBind.tvOpenAll.setOnClickListener {
            AllClickDialog.newInstance(true,object : AllClickDialog.OnClickListener{
                override fun onYes() {
                    ControlDevice.openAll()
                }
                override fun onNo() {
                }
            }).show(this.supportFragmentManager,"AllClickDialog1")
        }
        //一键全关
        mDataBind.tvCloseAll.setOnClickListener {
            AllClickDialog.newInstance(false,object : AllClickDialog.OnClickListener{
                override fun onYes() {
                    ControlDevice.closeAll()
                }
                override fun onNo() {
                }
            }).show(this.supportFragmentManager,"AllClickDialog2")
        }
        //灯光管理
        mDataBind.tvLight.setOnClickListener {
            ARouter.to(LightActivity::class.java)
        }
        //设备管理
        mDataBind.tvDevice.setOnClickListener {
            ARouter.to(DeviceActivity::class.java)
        }
        //欢迎词
        mDataBind.tvWelcome.setOnClickListener {
            ARouter.to(WelcomeActivity::class.java,13182)
        }
        //通用客户端
        mDataBind.tvLocal.setOnClickListener {
            ARouter.to(LocalActivity::class.java,13182)
        }
        //二级界面
        mDataBind.tvTwo.setOnClickListener {
            ARouter.to(SecondActivity::class.java,13182)
        }
        //开合屏
        mDataBind.tvDevice1.setOnClickListener {
            ARouter.to(OpenActivity::class.java,14013)
        }
        //方形柱
        mDataBind.tvDevice2.setOnClickListener {
            ARouter.to(LocalActivity::class.java,13933)
        }
        //过道屏
        mDataBind.tvDevice3.setOnClickListener {
            ARouter.to(LocalActivity::class.java,13934)
        }
        //梦想印记
        mDataBind.tvDevice4.setOnClickListener {
            ARouter.to(DreamActivity::class.java,13937)
        }
        //(企业地图)党建示范带
        mDataBind.tvDevice5.setOnClickListener {
            ARouter.to(MapActivity::class.java,13936)
        }
        //三破三立
        mDataBind.tvDevice6.setOnClickListener {
            ARouter.to(NetActivity::class.java,13935)
        }
    }


    override fun onResume() {
        super.onResume()
        //判断应用是否过期，如果过期弹窗禁止使用
        val dateLogin = SessionUtils.getObject<DateLogin>(CommonLib.Date_Login, DateLogin::class.java)
        if (dateLogin != null){
            val format: DateFormat = SimpleDateFormat("yyyy-MM-dd")
            val fwqTime = format.parse(dateLogin.date).time
            if (fwqTime < System.currentTimeMillis()){
                LoseAppDialog.newInstance(true,object :LoseAppDialog.OnClickListener{
                    override fun onF() {
                        finish()
                    }
                }).show(this.supportFragmentManager,"LoseAppDialog")
            }
        }else{
            LoseAppDialog.newInstance(false,object :LoseAppDialog.OnClickListener{
                override fun onF() {

                }
            }).show(this.supportFragmentManager,"LoseAppDialog2222")
        }

        mDataBind.tvDevice1.text  =  ControlDevice.getDeviceName(14013)
        mDataBind.tvDevice2.text  =  ControlDevice.getDeviceName(13933)
        mDataBind.tvDevice3.text  =  ControlDevice.getDeviceName(13934)
        mDataBind.tvDevice4.text  =  ControlDevice.getDeviceName(13937)
        mDataBind.tvDevice5.text  =  ControlDevice.getDeviceName(13936)
        mDataBind.tvDevice6.text  =  ControlDevice.getDeviceName(13935)

    }

}